package com.simin.base.appnet.encrypt;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Converter;


/**
 * Created by wsm on 2016/5/31.
 * 自定义响应ResponseBody
 */
public class JsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private final Gson mGson;//gson对象
    private final TypeAdapter<T> adapter;

    /**
     * 构造器
     */
    public JsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
        this.mGson = gson;
        this.adapter = adapter;
    }

    /**
     * 转换
     *
     * @param responseBody
     * @return
     * @throws IOException
     */
    @Override
    public T convert(ResponseBody responseBody) throws IOException {

        String response = responseBody.string();
         Log.i("convert","result 服务器获取加密结果：" + response);
        return adapter.fromJson(response);



//        //解密
//        if (response.toString().indexOf("{") != -1 || response.toString().indexOf("<html>") != -1) {
//            return adapter.fromJson(response.toString());
//        } else {
//            String result = new String(XXTEA.decryptWithBase64(response.getBytes(),
//                    HttpConstant.URL_CipherKey.getBytes()), "utf-8");
//              PickerLog.i("log","result" + "服务器解密的结果：" + result);
//            return adapter.fromJson(result);
//
//        }

    }

}
